package com.appheader.webfragmentviewpagertest.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.appheader.webfragmentviewpagertest.R;
import com.appheader.webfragmentviewpagertest.listener.OnHorizontalRecyclerViewListener;
import com.appheader.webfragmentviewpagertest.listener.OnRecyclerViewItemClickListener;
import com.appheader.webfragmentviewpagertest.viewholder.VerticalViewHolder;
import com.appheader.webfragmentviewpagertest.views.SpaceItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhep on 2016/9/28.
 */
public class HorizontalRecyclerAdapter extends RecyclerView.Adapter<VerticalViewHolder> {

    private Map<Integer, List<Integer>> mapList;
    private Context context;
    private OnHorizontalRecyclerViewListener horizontalListener;

    public static Map<String, Integer> map = new HashMap<>();
    public void setOnPositionListener(OnHorizontalRecyclerViewListener listener) {
        this.horizontalListener = listener;
    }
    public HorizontalRecyclerAdapter(Map<Integer, List<Integer>> mapList, Context context) {
        this.mapList = mapList;
        this.context = context;
    }
//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
////        RecyclerView.ViewHolder holder = null;
//
//        View view = LayoutInflater.from(context).inflate(R.layout.item_vertical_recycler, null);
//        VerticalViewHolder holder = new VerticalViewHolder(view);
//
//        return holder;
//    }
//
//    @Override
//    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//
//        if (holder instanceof VerticalViewHolder) {
//            List<Integer> itemList = mapList.get(position);
//            ((VerticalViewHolder) holder).recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
//            int spacingInPixels = context.getResources().getDimensionPixelSize(R.dimen.recycler_horizontal_margin);
//            ((VerticalViewHolder) holder).recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
//            VerticalRecyclerAdapter adapter = new VerticalRecyclerAdapter(itemList, context);
//            ((VerticalViewHolder) holder).recyclerView.setAdapter(adapter);
//
//            adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
//                @Override
//                public void onItemClick(View v,int vPosition) {
////                  Toast.makeText(context,"kkkkklllllll"+position,Toast.LENGTH_SHORT).show();
//                    Log.e("TAG", "onItemClick: " +position+"------"+ vPosition);
////                  horizontalListener.onItemPosition(position,vPosition);
//
//
//                }
//            });
//
//        }
//
//    }

    @Override
    public int getItemCount() {
        return mapList.size();
    }

//    class VerticalViewHolder extends RecyclerView.ViewHolder {
//
//        RecyclerView recyclerView;
//
//        public VerticalViewHolder(View itemView) {
//            super(itemView);
//            recyclerView = (RecyclerView) itemView.findViewById(R.id.vertical_recycler);
//        }
//    }



    @Override
    public VerticalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_vertical_recycler, null);
        VerticalViewHolder holder = new VerticalViewHolder(view, horizontalListener);

        return holder;
    }

    @Override
    public void onBindViewHolder(VerticalViewHolder holder, final int position) {
        if (holder instanceof VerticalViewHolder) {
            List<Integer> itemList = mapList.get(position);
            ((VerticalViewHolder) holder).recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

            int spacingInPixels = context.getResources().getDimensionPixelSize(R.dimen.recycler_horizontal_margin);
            ((VerticalViewHolder) holder).recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
            VerticalRecyclerAdapter adapter = new VerticalRecyclerAdapter(itemList, context);
            ((VerticalViewHolder) holder).recyclerView.setAdapter(adapter);

            adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View v, int vPosition) {
//                  Toast.makeText(context,"kkkkklllllll"+position,Toast.LENGTH_SHORT).show();
                    Log.e("TAG", "onItemClick: " + position + "------" + vPosition);
                    //TODO 在这里把值传出去
                  horizontalListener.onItemPosition(v,position,vPosition);
//                    map.put("hPosition", position);
//                    map.put("vPosition", vPosition);
                }
            });
        }
    }
//    public interface OnHorizontalRecyclerViewListener {
//        void onItemPosition(int hPosition,int vPosition);
//    }
}
